package m.common.action;

import m.system.RuntimeData;
import m.system.util.FileUtil;
import m.system.util.JSONMessage;

import java.io.File;
import java.io.InputStream;

public class VuePageResult extends VueResult {
	private JSONMessage param;
	private String url;
	public VuePageResult(String url){
		this(url,new JSONMessage());
	}
	public VuePageResult(String url,JSONMessage param) {
		this.url="/"+url;
		this.param=param;
	}
	public VuePageResult addParam(String name,Object value){
		this.param.push(name,value);
		return this;
	}
	public JSONMessage getParam() {
		return param;
	}
	public void setParam(JSONMessage param) {
		this.param = param;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	@Override
	public String outHtml() {
		String txt;
		try {
			File file;
			if(FileUtil.existsWebFile(this.url)) {//寻找web目录下文件
				file=new File(RuntimeData.getClassWeb()+this.url);
			}else if(FileUtil.existsPublicFile(this.url)) {//寻找web目录下文件
				file=new File(RuntimeData.getPublicWeb()+this.url);
			}else {//不存在则找临时目录
				String ps=RuntimeData.getClassTmp()+this.url;
				file=new File(ps);
				if(!file.exists()) {
					InputStream is=this.getClass().getClassLoader().getResourceAsStream("web"+this.url);
					if(null!=is) FileUtil.writeFile(ps, is);
					file=new File(ps);
				}
			}
			txt=FileUtil.read(file).trim();
		} catch (Exception e) {
			txt="{}";
		}
		return "{param:" + param + "," +
				txt.substring(1);
	}

	@Override
	public String getContent() {
		return new JSONMessage().push("url",url).push("param",param).toJSONString();
	}
}
